package day11;

import day10.supermarket.SaveThread;

import java.util.Random;

/**
 * @author oldliu
 * @since 1.0
 */
public class BankTest {
    public static void main(String[] args) {
        Account ac = new Account("wangkang", 4000);
        Taker t1 = new Taker("王康", ac);
        Taker t2 = new Taker("王康老婆", ac);
        Taker t3 = new Taker("王康小老婆", ac);
        Taker t4 = new Taker("王康小四", ac);
        Saver s = new Saver("王康老爸", ac);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        s.start();
    }
}

//取钱人
class Taker extends Thread {
    private final Account account;

    public Taker(String name, Account ac) {
        super(name);
        this.account = ac;
    }

    @Override
    public void run() {
        Random r = new Random();
        while (true) {
            int q = r.nextInt(400);
            System.out.println(this.getName() + "计划取钱" + q + "元！");
            try {
                synchronized (account) {
                    while (account.getMoney() < q) {
                        System.out.println(this.getName() + "计划取钱" + q + "元,账户" + account.getMoney() + "钱不够，等着！");
                        account.wait();//无线等待
                        System.out.println(this.getName() + "被喊醒！");
                    }
                    Thread.sleep(40);
                    account.takeMoney(q);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


            System.out.println(this.getName() + "取钱" + q + "元后，余额是:" + account.getMoney() + "！");

        }
    }
//    @Override
//    public void run() {
//        Random r = new Random();
//        while (this.account.getMoney() > 0) {
//            int q = r.nextInt(100);
//            System.out.println(this.getName() + "计划取钱" + q + "元！");
//
//                if (account.getMoney() >= q) {
//                    try {
//                        Thread.sleep(40);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    account.takeMoney(q);
//                }
//
//            System.out.println(this.getName() + "取钱" + q + "元后，余额是:" + account.getMoney() + "！");
//
//        }
//    }
}

//存钱人
class Saver extends Thread {
    private final Account accout;

    public Saver(String name, Account ac) {
        super(name);
        this.accout = ac;
    }

    @Override
    public void run() {
        while (true) {
            try {
                sleep(4000);
                synchronized (accout) {
                    this.accout.deposit(3500);
                    System.out.println(this.getName() + "存钱后，账户余额是" + this.accout.getMoney() + "元");

                    accout.notifyAll();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

//账号
class Account {
    private String no;
    private int money;

    public Account() {
    }

    public Account(String no, int money) {
        this.no = no;
        this.money = money;
    }

    //取钱...
    public void takeMoney(int m) {
        this.money -= m;
    }

    public void deposit(int m) {
        this.money += m;
    }

    public String getNo() {
        return no;
    }


    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}